package com.len.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @Author: xudeli
 * @Description:
 * @CreateDate: 2018/3/22 23:48
 **/
public class CheckFieldUtils {
    private static final Logger logger = LoggerFactory.getLogger(CheckFieldUtils.class);

    public static StringBuffer checkObj(Object obj1, Object obj2, String[] compareFields) {
        StringBuffer resultStr = new StringBuffer();
        Class<?> cls1 = obj1.getClass();
        Class<?> cls2 = obj2.getClass();
        Field[] objectFields = null;
        try {
            objectFields = cls1.getDeclaredFields();
            if (null != objectFields && objectFields.length > 0) {
                for (Field obj1Field : objectFields) {
                    String name = obj1Field.getName();
                    for (String compareField : compareFields) {
                        if (compareField.trim().equalsIgnoreCase(name.trim())) {
                            Object value1 = cls1.getDeclaredMethod("get" + toUperFirst(name), null).invoke(obj1, null);
                            Object value2 = cls2.getDeclaredMethod("get" + toUperFirst(name), null).invoke(obj2, null);
                            if (null != value1 && value1.equals(value2)) {
                                logger.info(obj1Field.getName() + ":" + value1);
                            } else {
                                logger.info(obj1Field.getName() + ":" + value1 + "与" + value2);
                                resultStr.append(name + "\n");
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("occur:" + e.getMessage(), e);
        }
        return resultStr;
    }

    private static String toUperFirst(String name) {
        if (null != name) {
            StringBuffer sb = new StringBuffer(name);
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
            return sb.toString();
        }
        return null;
    }


    public static void main(String[] args) {
        String[] compareFields = new String[]{"seckillId","code1", "code2", "code3", "code4","code5","code6"};
        String[] bondCompareFields = new String[]{"code1", "code2"};
        String[] strings = Arrays.copyOf(compareFields, compareFields.length+bondCompareFields.length,bondCompareFields.getClass());
//        Seckill seckill = new Seckill();
//        seckill.setSeckillId(Long.parseLong(RandomStringUtils.randomNumeric(5)));
//        SuccessKilled successKilled = new SuccessKilled();
//        successKilled.setSeckillId(Long.parseLong(RandomStringUtils.randomNumeric(5)));
//        checkObj(seckill,successKilled,compareFields);
        //System.out.println(strings);
        //compareFields = Arrays.copyOf(compareFields, compareFields.length, bondCompareFields.length);
        /*for (int i = 0; i < bondCompareFields.length; i++) {
            compareFields[(compareFields.length - bondCompareFields.length) + i] = bondCompareFields[i];
        }*/
        //System.out.println(Arrays.toString(compareFields));
    }
}